package com.example.zegodemo;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import androidx.core.app.NotificationCompat;

public class ForegroundNotificationService extends Service {

    // Android 7.0 之后，显示通知时指定通知频道，由于管理通知类型
    public static final String FOREGROUND_NOTIFICATION_CHANNEL_ID = "zego_live_id";
    public static final String FOREGROUND_NOTIFICATION_CHANNEL_NAME = "kiki live";

    // 前台通知ID
    private static final int FOREGROUND_NOTIFICATION_ID = 111;
//    private NotificationManager mNotificationManager;

    @Override
    public void onCreate() {
        super.onCreate();

        // 获取 NotificationManager 对象，用户显示通知 
//         mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        createNotificationChannel();
        // 显示通知，并且设置当前服务为前台服务
        showNotification();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // 业务方可以在这里做一些业务相关的逻辑
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 业务方可以在这里做一些业务相关的逻辑
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 当服务被销毁的同时，也取消对应的通知 
//        mNotificationManager.cancel(FOREGROUND_NOTIFICATION_ID);
        stopForeground(true);
        stopSelf();
    }

    /**
     * 创建通知频道
     */
    private void createNotificationChannel() {
        // 创建前台服务通知频道
        NotificationChannel channel = new
                NotificationChannel(ForegroundNotificationService.FOREGROUND_NOTIFICATION_CHANNEL_ID,
                ForegroundNotificationService.FOREGROUND_NOTIFICATION_CHANNEL_NAME,
                NotificationManager.IMPORTANCE_NONE);
        channel.setDescription("demo测试前台服务通知");
        NotificationManager notificationManager =
                getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

    private void showNotification() {

//        PendingIntent pendingIntent = PendingIntent.getActivity(this,
//                FOREGROUND_NOTIFICATION_PENDING_INTENT_REQUEST_CODE, // PendingIntent 的请求码
//                new Intent(this, XXXActivity.class), // 添加通知栏要跳转的⻚面
//                PendingIntent.FLAG_UPDATE_CURRENT); // 这里使用 FLAG_UPDATE_CURRENT，用户需按需选择 flag

        Notification notification = new NotificationCompat.Builder(this,
                FOREGROUND_NOTIFICATION_CHANNEL_ID)
                .setContentText("kiki live") // 通知的文案
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
//                .setContentIntent(pendingIntent) // 点击后触发的 intent
                .setSmallIcon(R.mipmap.ic_launcher) // 通知的小图标
                .setCategory(Notification.CATEGORY_SERVICE)
                .setWhen(System.currentTimeMillis())// 设置该通知发生的时间
                .build();

        // 显示通知(前台服务需要依赖于通知)
//        mNotificationManager.notify(FOREGROUND_NOTIFICATION_ID, notification);
        // 将当前服务设置为前台服务
        startForeground(FOREGROUND_NOTIFICATION_ID, notification);
    }
}